package com.luxf.base.domainevent.convert;

import com.google.gson.Gson;

import javax.persistence.AttributeConverter;

/**
 * @author 小66
 * @create 2021-08-22 11:08
 **/
public abstract class JsonConverter<T> implements AttributeConverter<T, String> {
    private final Class<T> entityClass;

    private final Gson gson = new Gson();

    protected JsonConverter(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    @Override
    public String convertToDatabaseColumn(T eventNotice) {
        return gson.toJson(eventNotice);
    }

    @Override
    public T convertToEntityAttribute(String json) {
        return gson.fromJson(json, entityClass);
    }
}
